/*
 * @Author: Yanan Chen
 * @Date: 2021-11-02 15:04:40
 * @LastEditTime: 2021-11-08 13:54:49
 * @LastEditors: Yanan Chen
 * @Description: 
 * @FilePath: \esp32\Src\Hal\hal_pwm.c
 * 
 */

#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_attr.h"

#include "driver/mcpwm.h"
#include "soc/mcpwm_periph.h"
#include "driver/pcnt.h"
#include "hal_pwm.h"

static void mcpwm_gpio_initialize(void)
/**
 * @description: 
 * @param {*}
 * @return {*}
 */
{
    mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, PWM_PULSE_GPIO); //
}


int pwm_init()
/**
 * @description: 
 * @param {*}
 * @return {*}
 */
{
    mcpwm_gpio_initialize();
     mcpwm_config_t pwm_config = {
        .frequency = 1000, // frequency = 1000HZ
        .cmpr_a = 50,     // duty cycle of PWMxA = 0
        .counter_mode = MCPWM_UP_COUNTER,
        .duty_mode = MCPWM_DUTY_MODE_0,
    };
    mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);
}


int pwm_set(unsigned char value)
{
    value = (value>100)?(100):(value);
    mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, value);
}


